<?php
cmspad_import('database.DatabaseDriver');
cmspad_import('Logging');
class DatabaseGroup{

	/**
	 * 数据库类型
	 *
	 * @var string
	 */
	private $type;
	
	/**
	 * 存放数据库配置信息的数组
	 *
	 * @var array
	 */
	private $configurations;

	/**
	 * 数据库连接
	 *
	 * @var Database
	 */
	private $connection;

	public function __construct($type = 'mysql'){
		$this->type = $type;
		$this->configurations = array();
		$this->connection = NULL;
	}

	/**
	 * 添加一个数据库配置信息
	 *
	 * @param string|int $id 配置信息ID
	 * @param string $host 数据库主机抵制
	 * @param string $name 数据库名称
	 * @param string $username 登陆账号
	 * @param string $password 登陆密码
	 * @param int $port 连接端口
	 * @param string $encoding 字符编码
	 */
	public function add($id, $host, $name, $username, $password, $port = 3306, $encoding = 'utf8'){
		$this->configurations[$id] = array($host, $name, $username, $password, $port, $encoding);
	}

	/**
	 * 删除一个数据库配置信息
	 *
	 * @param string|int $id 配置信息ID
	 */
	public function del($id){
		unset($this->configurations[$id]);
	}
	
	/**
	 * 获取数据库配置信息数量
	 *
	 * @return int
	 */
	public function getDatabaseCount(){
		return count($this->configurations);
	}

	/**
	 * @see DatabaseConnection#isConnected
	 */
	public function isConnected(){
		return $this->connection !== NULL && $this->connection->isConnected();
	}

	/**
	 * 连接数据库
	 *
	 * @param string|int $id 指定配置信息ID，如果不指定则随机指定数据库配置信息.
	 * @return bool 返回TRUE说明数据库已经连接成功，否则无法连接所有数据库
	 */
	public function connect($id = false){
		if($this->connection !== NULL){
			if($id !== false){
				$this->disconnect();
			}else{
				return true;
			}
		}
		if($id === false){
			// 创建随机数据库连接
			$connection = new DatabaseDriver($this->type);

			// 如果没成功或者没有初始化，则随机寻找可用数据库
			$keys = array_keys($this->configurations);
			for($l = count($keys), $i = 0;$i < $l;$i++){
				$configuration = $this->configurations[$keys[$i]];
				try{
					if($connection->connect($configuration[0], $configuration[1], $configuration[2], $configuration[3], $configuration[4], $configuration[5]) != false){
						$this->connection = $connection;
						return true;
					}
				}catch(DatabaseException $e){
					$logging = Logging::getInstance();
					$logging->warn('DatabaseGroup; Can not connect database("'.$keys[$i].'"): '.$e->getMessage(), __FILE__, __LINE__);
				}
			}
			return false;
		}else{
			// 创建固定数据库连接
			$configuration = $this->configurations[$id];
			$connection = new DatabaseDriver($this->type);
			if($connection->connect($configuration[0], $configuration[1], $configuration[2], $configuration[3], $configuration[4], $configuration[5]) === true){
				$this->connection = $connection;
				return true;
			}else{
				return false;
			}
		}
	}

	/**
	 * @see DatabaseConnection#addBatch
	 */
	public function addBatch($sql){
		$this->_checkConnection();
		$this->connection->addBatch($sql);
	}

	/**
	 * @see DatabaseConnection#executeBatch
	 */
	public function executeBatch(){
		$this->_checkConnection();
		return $this->connection->executeBatch();
	}

	/**
	 * @see DatabaseConnection#query
	 */
	public function query($sql, $length = 0, $offset = 0){
		$this->_checkConnection();
		return $this->connection->query($sql, $length, $offset);
	}

	/**
	 * @see DatabaseConnection#update
	 */
	public function update($sql, $length = 0, $offset = 0){
		$this->_checkConnection();
		return $this->connection->update($sql, $length, $offset);
	}

	/**
	 * @see DatabaseConnection#prepare
	 */
	public function prepare($sql, $length = 0, $offset = 0){
		$this->_checkConnection();
		return $this->connection->prepare($sql, $length, $offset);
	}

	/**
	 * @see DatabaseConnection#disconnect
	 */
	public function disconnect(){
		if($this->connection !== NULL){
			$this->connection->disconnect();
			$this->connection = NULL;
		}
	}

	/**
	 * 检查数据库是否已连接
	 */
	private function _checkConnection(){
		if($this->connection === NULL){
			throw new DatabaseException(900123, 'Database has not connected yet');
		}
	}
}
?>